package com.teamwe.movecard.controller;

import com.teamwe.movecard.domain.Result;
import com.teamwe.movecard.domain.Score;
import com.teamwe.movecard.domain.User;
import com.teamwe.movecard.service.ScoreService;
import com.teamwe.movecard.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "排行榜数据接口")
@Slf4j
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private  ScoreService scoreService;

    @Autowired
    private  UserService userService;

    /**
     * 功能描述：获取排行榜数据（前五名）
     * @return
     */
    @ApiOperation(value = "获取排行版数据", notes = "获取排行榜前五名的的用户数据，包括用户信息和用户分数")
    @GetMapping("/all")
    public Result getScoreList(){
        Result result = new Result();
        List<Score> rankList = null;
        try {
            rankList = scoreService.getRankList();
            result.setStatus(true);
            result.addParam("rankList", rankList);
            result.setDescribe("查询成功");
            return result;
        } catch (Exception e) {
            result.setStatus(false);
            result.setDescribe("查询失败");
            return result;
        }
    }

    /**
     * 功能描述：获取个人排行数据
     * @return
     */
    @ApiOperation(value = "获取个人排行榜数据", notes = "通过用户的openId查询用户的排行榜信息，可以获取用户的排行榜信息，可以获取到用户在排行榜的位置")
    @GetMapping()
    public Result getScorePersonal(@RequestParam String mpOpenId){
        Result result = new Result(true);
        //历史累积的的答题分数
        Score score = null;
        try {
            score = scoreService.getUserScore(mpOpenId);
            if (score != null) {
                result.addParam("scoreInfo", score);
                Integer rank = scoreService.getRankByOpenId(mpOpenId);
                result.addParam("rank", rank);
                String describe = "收到的请求参数为 mpOpenId:" + mpOpenId;
                result.setDescribe(describe);
            }else{
                //获取到的分数是空的，说明数据库中没有该用户的信息
                result.setStatus(false);
                result.addParam("rank", -1);
                String describe = "用户不存在，收到的请求参数为 myOpenId:" + mpOpenId;
                result.setDescribe(describe);
            }
            return  result;
        } catch (Exception e) {
            result.setStatus(false);
            try {
                User user = userService.getUser(mpOpenId);
                result.setDescribe(String.format("查询失败！收到的请求参数为 mpOpenId: %s ",mpOpenId));
            } catch (Exception ex) {
                result.setDescribe(String.format("该用户不存在！收到的请求参数为 mpOpenId: %s ",mpOpenId));
            }finally {
                return result;
            }
        }
    }

    /**
     * 功能描述：修改个人答对题目的数据，提交的分数会进行累加到以往的答题成绩
     * @param score
     * @return
     */
    @ApiOperation(value = "提交分数", notes = "提交答题之后的分数，然后分数会累加到以往的分数之上")
    @PostMapping
    public Result submitScore(@ApiParam(value = "分数", required = true) @RequestBody Score score){
        Result result = new Result(true);
        String msg = null;
        if (score.getMpOpenId() == null || score.getMpOpenId().trim().equals("")) {
            result.setStatus(false);
            msg = "分数提交失败!mpOpemId不能为空";
            result.setDescribe(String.format("%s 收到的参数为： mpOpenId: %s, score: %d", msg, score.getMpOpenId(), score.getScore()));
            return result;
        }else{
            try {
                scoreService.saveScore(score);
                msg = "分数提交成功!";
            } catch (Exception e) {
                result.setStatus(false);
                msg = "分数提交失败!";
            }finally {
                result.setDescribe(String.format("%s 收到的参数为： mpOpenId: %s, score: %d", msg, score.getMpOpenId(), score.getScore()));
                return result;
            }
        }
    }
}
